<?php
defined('SYSPATH') or die('No direct script access.');

class Model_Images extends Model {
    protected $_table = 'images';
    protected $_quantity = 12; // колличество элементов на странице

    public function add_image($array)
    {
        $sql = "INSERT INTO ". $this->_table."
        (path_to_fullsize , path_to_thumb, description, upload_date)
        VALUES('".$array['path_to_fullsize']."','".$array['path_to_thumb']."','".$array['description']."', NOW())";
        return DB::query(Database::INSERT, $sql)
            ->execute();
    }

    public function get_page($page)
    {
        $start=($page-1)*$this->_quantity;
        $sql = "SELECT id, path_to_thumb FROM ".$this->_table." ORDER BY id DESC LIMIT ".$start.",".$this->_quantity;
        $result=DB::query(Database::SELECT, $sql)
            ->execute()->as_array();
        return $result;
    }

    public function get_max_pages()
    {
        $sql="SELECT COUNT(*) AS count FROM ".$this->_table;
        $result=DB::query(Database::SELECT, $sql)
            ->execute()->as_array();
        $result=$result[0]['count'];
        $pages=ceil($result/$this->_quantity);
        return $pages;
    }

    public function get_image_info($id)
    {
        $sql="SELECT path_to_fullsize, description FROM ".$this->_table." WHERE id=".$id;
        $result=DB::query(Database::SELECT, $sql)
            ->execute()->as_array();
        return $result;
    }

}